package com.fmi.cloud.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;


public class YcWebView extends WebView {


    private Context mContext;
    String head = null;
    String table = null;
    String images = null;
    String html = "";
    String content = null;

    public YcWebView(Context context) {
        super(context);
        this.mContext = context;
        this.setWebViewClient(new WebViewClient());
    }

    public YcWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        this.setWebViewClient(new WebViewClient());
    }

    public YcWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        this.setWebViewClient(new WebViewClient());
    }

    @Override
    public void loadDataWithBaseURL(String baseUrl, String data,
                                    String mimeType, String encoding, String historyUrl) {
        super.loadDataWithBaseURL(baseUrl, data, "text/html", "UTF-8",
                historyUrl);
    }

    /**
     * 通过WebView展示内容
     *
     * @param desc
     * @return
     */
    public void loadDescWithBaseURL(String title, String desc) {
        html = getHtmlTxt().replace("<!title>", title);
        String[] data = desc.split("<img");
        desc = data[0];
        for (int i = 1; i < data.length; i++) {
            desc += "<div width=\"100%\" class=\"div\"><img style = \"width:100%\"" + data[i] + "</div>";
        }
        if (desc == null || "".equals(desc) || "null".equals(desc)) {
            html = html.replace("<!content>", "");
        } else {
            html = html.replace("<!content>", desc);
        }

        Log.e("ttttt", html);
        loadDataWithBaseURL(getHtmlTxt(), html, null, null, null);
    }

    /**
     * 通过WebView展示内容,展示富文本框的内容
     *
     * @param des
     * @return
     */
    public void loadDescWithBaseURL(String des) {
        // html = html.replace("<!content>", des);
        // Log.e("ttttt", html);
        String[] data = des.split("<img");
        int end = data[0].length() - 1;
        String data1 = data[0].substring(0, end);
        data1 = data1 + " style = \"text-align:center\">";
        des = data1;
        for (int i = 1; i < data.length; i++) {
            des += "<div width=\"100%\" class=\"div\"><img  style = \"width:100%\"" + data[i];
            if (i < data.length - 1) {
                end = des.length() - 1;
                data1 = des.substring(0, end);
                data1 = data1 + "style = \"text-align:center\"></div>";
                des = data1;
            }

        }
        loadDataWithBaseURL(getHtmlTxt(), des, null, null, null);
    }

    private String Table(Map<String, Object> map) {

        Iterator iter = map.entrySet().iterator();
        StringBuffer sb = new StringBuffer();
        while (iter.hasNext()) {
            Entry entry = (Entry) iter.next();
            sb.append("<tr><td>" + entry.getKey().toString()
                    + entry.getValue().toString() + "</td></tr>");
        }

        return sb.toString();
    }

    private String img(String[] args) {
        StringBuffer sb = new StringBuffer();
        String str[] = args;
        for (String img : str) {
            sb.append("<div width=\"100%\" class=\"div\"><img  class=\"img\" src=\"" + img + "-md.jpg " + "\"/></div>");
            sb.append("\n");
        }

        return sb.toString();
    }

    private String img_(String[] args) {
        StringBuffer sb = new StringBuffer();
        String str[] = args;
        for (String img : str) {
            sb.append("<div width=\"100%\" class=\"div\"><img  class=\"img\" src=\"" + img + "\"/></div>");
        }

        return sb.toString();
    }

    private String getHtmlTxt() {
        String html = "";
        try {
            InputStreamReader inputReader = new InputStreamReader(mContext
                    .getAssets().open("html/html.xml"));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String Result = "";
            while ((line = bufReader.readLine()) != null) {
                Result += line;
                Result += "\n";
            }
            return Result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }

    @Override
    public void loadUrl(String url) {
        super.loadUrl(url);
    }

    @SuppressWarnings("unused")
    private class OpenWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("tel:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(url));
                mContext.startActivity(intent);
                return true;
            } else {
                loadUrl(url);
                return true;
            }
        }
    }


}
